using System;
using System.Collections;
using System.Collections.Generic;
using OracleInternal.Common;
using OracleInternal.I18N;
using OracleInternal.Network;
using OracleInternal.ServiceObjects;

namespace OracleInternal.TTC
{
	internal class MarshallingEngine
	{
		private const int TTCC_MXL = 252;

		internal const int TTCC_ESC = 253;

		internal const int TTCC_LNG = 254;

		internal const int TTCC_ERR = 255;

		internal const int TTCC_MXIN_NEW = 32767;

		internal const int TTCC_MXIN_OLD = 64;

		internal int m_effectiveTTCC_MXIN = 64;

		internal const byte TTCLXMULTI = 1;

		internal const byte TTCLXMCONV = 2;

		private const int FLUSH_DATA_SIZE_THRESHOLD = 65536;

		internal int m_numOBThresholdForSends = 1;

		private int m_DBVersion;

		private bool m_bHasFSAPCapability;

		internal bool m_hasEOCSCapability;

		internal bool m_bUseBigCLRChunks;

		internal bool m_bServerUsingBigSCN;

		internal TTCTypeRepresentation m_typeRepresentation;

		internal OracleCommunication m_oracleCommunication;

		internal byte m_negotiatedTTCVersion;

		internal long m_endOfCallStatus;

		internal bool m_bDRCPConnection;

		internal bool m_bDRCPSessionAttached;

		internal int m_endToEndECIDSequenceNumber;

		internal byte[] m_ltxId;

		internal byte[] ignored = new byte[255];

		internal byte[] tmpBuffer2 = new byte[2];

		internal byte[] tmpBuffer4 = new byte[4];

		internal byte[] tmpBuffer6 = new byte[6];

		internal byte[] tmpBuffer8 = new byte[8];

		internal int[] retLen = new int[1];

		private TTCError m_ttcError;

		internal OraBufReader m_oraBufRdr;

		internal OraBufWriter m_oraBufWriter;

		internal TTCSessionGet m_drcpSessionGet;

		internal TTCSessionRelease m_drcpSessionRelease;

		internal TTCSessionReturnValues m_drcpSessionReturnValues;

		internal Conv m_dbCharSetConv;

		internal Conv m_nCharSetConv;

		internal bool m_bSvrCSMultibyte;

		internal CharArrayPooler m_charArrayPooler;

		internal OracleConnectionImpl m_connImplReference;

		internal byte NegotiatedTTCVersion
		{
			get
			{
				return m_negotiatedTTCVersion;
			}
			set
			{
				m_negotiatedTTCVersion = value;
			}
		}

		internal TTCError TTCErrorObject
		{
			get
			{
				if (m_ttcError == null)
				{
					m_ttcError = new TTCError(this);
				}
				return m_ttcError;
			}
			set
			{
				m_ttcError = value;
			}
		}

		internal bool HasFSAPCapability
		{
			get
			{
				return m_bHasFSAPCapability;
			}
			set
			{
				m_bHasFSAPCapability = value;
			}
		}

		internal bool HasEOCSCapability
		{
			get
			{
				return m_hasEOCSCapability;
			}
			set
			{
				m_hasEOCSCapability = value;
			}
		}

		internal int DBVersion
		{
			get
			{
				return m_DBVersion;
			}
			set
			{
				m_DBVersion = value;
			}
		}

		internal MarshallingEngine(OracleCommunication communication, OracleConnectionImpl connImplReference)
		{
			m_connImplReference = connImplReference;
			m_oracleCommunication = communication;
			m_oraBufWriter = new OraBufWriter(this, communication.m_sessionCtx.m_writerStream, communication);
			m_oraBufRdr = new OraBufReader(communication.m_sessionCtx.m_readerStream, m_oraBufWriter);
			m_typeRepresentation = new TTCTypeRepresentation();
			m_typeRepresentation.m_representationArray[1] = 2;
			m_charArrayPooler = new CharArrayPooler(2, 32768);
			if (65536 > communication.SDU)
			{
				m_numOBThresholdForSends = 65536 / communication.SDU;
			}
		}

		internal void MarshalUB1(short val)
		{
			if (m_oraBufWriter.m_currentOB.Space - m_oraBufWriter.m_positionInCurrentOB >= 1)
			{
				m_oraBufWriter.m_currentObBuffer[m_oraBufWriter.m_positionInCurrentOB] = (byte)val;
				m_oraBufWriter.m_positionInCurrentOB++;
				m_oraBufWriter.m_lengthForDataSegment++;
			}
			else
			{
				m_oraBufWriter.Write((byte)((uint)val & 0xFFu));
			}
		}

		internal void MarshalUB2(int val)
		{
			MarshalSB2((short)(val & 0xFFFF));
		}

		internal void MarshalNativeUB2(int val)
		{
			byte b = m_typeRepresentation.m_representationArray[1];
			m_typeRepresentation.m_representationArray[1] = 0;
			MarshalUB2(val);
			m_typeRepresentation.m_representationArray[1] = b;
		}

		internal void MarshalUWORD(long value)
		{
			MarshalSB4((int)(value & 0xFFFFFFFFu));
		}

		internal void MarshalUB4(long val)
		{
			MarshalSB4((int)(val & 0xFFFFFFFFu));
		}

		internal void MarshalO2U(bool notnull)
		{
			if (notnull)
			{
				AddPointer(1);
			}
			else
			{
				AddPointer(0);
			}
		}

		internal void MarshalPointer()
		{
			AddPointer(1);
		}

		internal void MarshalNullPointer()
		{
			AddPointer(0);
		}

		internal short UnmarshalUB1(bool bIgnoreData = false)
		{
			return (short)m_oraBufRdr.Read(bIgnoreData);
		}

		internal void MarshalSB2(short val)
		{
			byte b = ValueToBuffer(val, tmpBuffer2, 1);
			if (b != 0)
			{
				m_oraBufWriter.Write(tmpBuffer2, 0, b);
			}
		}

		internal void MarshalSB4(int val)
		{
			byte b = ValueToBuffer(val, tmpBuffer4, 2);
			if (b != 0)
			{
				m_oraBufWriter.Write(tmpBuffer4, 0, b);
			}
		}

		internal void MarshalSB8(long val)
		{
			byte b = ValueToBuffer(val, tmpBuffer8, 3);
			if (b != 0)
			{
				m_oraBufWriter.Write(tmpBuffer8, 0, b);
			}
		}

		internal void MarshalSWORD(int value)
		{
			MarshalSB4(value);
		}

		internal void MarshalDALC(byte[] buffer)
		{
			if (buffer == null || buffer.Length < 1)
			{
				m_oraBufWriter.Write(0);
				return;
			}
			MarshalSB4((int)(0xFFFFFFFFu & buffer.Length));
			MarshalCLR(buffer, buffer.Length);
		}

		internal void MarshalCHR(byte[] value)
		{
			MarshalCHR(value, 0, value.Length);
		}

		internal void MarshalCHR(byte[] value, int offset, int length)
		{
			if (length > 0)
			{
				if (m_typeRepresentation.ConversionRequired)
				{
					MarshalCLR(value, offset, length);
				}
				else
				{
					m_oraBufWriter.Write(value, offset, length);
				}
			}
		}

		internal void MarshalBytes(byte[] value, int offset, int length)
		{
			m_oraBufWriter.Write(value, offset, length);
		}

		internal void MarshalUB4Array(long[] value)
		{
			for (int i = 0; i < value.Length; i++)
			{
				MarshalSB4((int)(value[i] & 0xFFFFFFFFu));
			}
		}

		internal void MarshalKEYVAL(byte[][] keys, byte[][] values, byte[] kvalflg, int nb)
		{
			for (int i = 0; i < nb; i++)
			{
				if (keys[i] != null && keys[i].Length != 0)
				{
					MarshalUB4(keys[i].Length);
					MarshalCLR(keys[i], 0, keys[i].Length);
				}
				else
				{
					MarshalUB4(0L);
				}
				if (values[i] != null && values[i].Length != 0)
				{
					MarshalUB4(values[i].Length);
					MarshalCLR(values[i], 0, values[i].Length);
				}
				else
				{
					MarshalUB4(0L);
				}
				if (kvalflg[i] != 0)
				{
					MarshalUB4(1L);
				}
				else
				{
					MarshalUB4(0L);
				}
			}
		}

		internal void MarshalCLR(byte[] value, int valueLen)
		{
			MarshalCLR(value, 0, valueLen);
		}

		internal byte[] UnmarshalCLR(int buflen, int[] intArray)
		{
			byte[] array = new byte[buflen];
			UnmarshalCLR(array, 0, intArray, buflen);
			return array;
		}

		internal void MarshalCLR(byte[] value, int offset, int valueLen)
		{
			if (valueLen > 252)
			{
				int num = 0;
				m_oraBufWriter.Write(254);
				do
				{
					int num2 = valueLen - num;
					int num3 = ((num2 > m_effectiveTTCC_MXIN) ? m_effectiveTTCC_MXIN : num2);
					if (m_bUseBigCLRChunks)
					{
						MarshalSB4(num3);
					}
					m_oraBufWriter.WriteLengthAndDataToOraBuf(!m_bUseBigCLRChunks, num3, value, offset + num, num3);
					num += num3;
				}
				while (num < valueLen);
				m_oraBufWriter.Write(0);
			}
			else if (value.Length != 0)
			{
				m_oraBufWriter.WriteLengthAndDataToOraBuf(bWritePrefixLength: true, valueLen, value, offset, valueLen);
			}
			else
			{
				m_oraBufWriter.Write((byte)((uint)valueLen & 0xFFu));
			}
		}

		internal void UnmarshalCLR(byte[] bytes, int offsetRow, int[] intArray)
		{
			UnmarshalCLR(bytes, offsetRow, intArray, int.MaxValue);
		}

		internal void UnmarshalCLR(byte[] bytes, int offsetRow, int[] intArray, int maxSize)
		{
			int num = 0;
			int num2 = 0;
			int num3 = offsetRow;
			int num4 = 0;
			int num5 = 0;
			bool flag = false;
			int num6 = -1;
			num = UnmarshalUB1();
			if (num < 0)
			{
				throw new Exception("TTC Error");
			}
			if (num == 0)
			{
				intArray[0] = 0;
				return;
			}
			if (EscapeSequenceNull(num))
			{
				intArray[0] = 0;
				return;
			}
			if (num != 254)
			{
				if (num > 0)
				{
					num5 = ((maxSize - num4 < num) ? (maxSize - num4) : num);
					num3 = UnmarshalBuffer(bytes, num3, num5);
					num4 += num5;
					int num7 = num - num5;
					if (num7 > 0)
					{
						UnmarshalBuffer(ignored, 0, num7);
					}
				}
			}
			else
			{
				num6 = -1;
				bool flag2 = false;
				while (!flag2)
				{
					if (num6 != -1)
					{
						num = ((!m_bUseBigCLRChunks) ? UnmarshalUB1() : UnmarshalSB4());
						if (num <= 0)
						{
							flag2 = true;
							continue;
						}
					}
					if (num == 254)
					{
						switch (num6)
						{
						case -1:
							num6 = 1;
							continue;
						case 1:
							num6 = 0;
							break;
						case 0:
							if (flag)
							{
								num6 = 0;
								break;
							}
							num6 = 0;
							continue;
						}
					}
					if (num3 == -1)
					{
						UnmarshalBuffer(ignored, 0, num);
					}
					else
					{
						num2 = num;
						if (num2 > 0)
						{
							num5 = ((maxSize - num4 < num2) ? (maxSize - num4) : num2);
							num3 = UnmarshalBuffer(bytes, num3, num5);
							num4 += num5;
							int num8 = num2 - num5;
							if (num8 > 0)
							{
								UnmarshalBuffer(ignored, 0, num8);
							}
						}
					}
					num6 = 0;
					if (num > 252)
					{
						flag = true;
					}
				}
			}
			if (intArray != null)
			{
				if (num3 != -1)
				{
					intArray[0] = num4;
				}
				else
				{
					intArray[0] = bytes.Length - offsetRow;
				}
			}
		}

		internal int UnmarshalBuffer(byte[] _byteValue, int offset, int len)
		{
			if (len <= 0)
			{
				return offset;
			}
			if (_byteValue.Length < offset + len)
			{
				UnmarshalNBytes(_byteValue, offset, _byteValue.Length - offset);
				UnmarshalNBytes(ignored, 0, offset + len - _byteValue.Length);
				offset = -1;
			}
			else
			{
				UnmarshalNBytes(_byteValue, offset, len);
				offset += len;
			}
			return offset;
		}

		internal int UnmarshalSB4()
		{
			return (int)UnmarshalUB4();
		}

		internal long UnmarshalSB8()
		{
			return BufferToValue(3);
		}

		internal void UnmarshalKPDKV(byte[][] textValues, int[] textValuesLength, byte[][] binaryValues, int[] keywords)
		{
			int num = 0;
			int[] array = new int[1];
			for (int i = 0; i < textValues.Length; i++)
			{
				num = (int)UnmarshalUB4();
				if (num > 0)
				{
					textValues[i] = new byte[num];
					UnmarshalCLR(textValues[i], 0, array, num);
					textValuesLength[i] = array[0];
				}
				num = (int)UnmarshalUB4();
				if (num > 0)
				{
					binaryValues[i] = new byte[num];
					UnmarshalCLR(binaryValues[i], 0, array, num);
				}
				keywords[i] = UnmarshalUB2();
			}
		}

		internal int[] UnmarshalKEYVAL(byte[][] keys, byte[][] values, int nb)
		{
			byte[] array = new byte[1000];
			int[] array2 = new int[1];
			int[] array3 = new int[nb];
			for (int i = 0; i < nb; i++)
			{
				if (UnmarshalSB4() > 0)
				{
					UnmarshalCLR(array, 0, array2);
					keys[i] = new byte[array2[0]];
					Buffer.BlockCopy(array, 0, keys[i], 0, array2[0]);
				}
				if (UnmarshalSB4() > 0)
				{
					UnmarshalCLR(array, 0, array2);
					values[i] = new byte[array2[0]];
					Buffer.BlockCopy(array, 0, values[i], 0, array2[0]);
				}
				array3[i] = UnmarshalSB4();
			}
			return array3;
		}

		internal byte UnmarshalSB1()
		{
			return (byte)UnmarshalUB1();
		}

		internal void MarshalB1Array(byte[] inputBuffer)
		{
			if (inputBuffer.Length != 0)
			{
				m_oraBufWriter.Write(inputBuffer);
			}
		}

		internal void MarshalB1Array(byte[] value, int off, int len)
		{
			if (value.Length != 0)
			{
				m_oraBufWriter.Write(value, off, len);
			}
		}

		internal short UnmarshalSB2()
		{
			return (short)UnmarshalUB2();
		}

		internal int UnmarshalUB2(bool bIgnoreData = false)
		{
			return (int)(BufferToValue(1, bIgnoreData) & 0xFFFF);
		}

		internal long UnmarshalUB4(bool bIgnoreData = false)
		{
			return BufferToValue(2, bIgnoreData);
		}

		internal byte[] UnmarshalTEXT(int length)
		{
			int num = 0;
			byte[] array = new byte[length];
			while (num < length)
			{
				if (m_oraBufRdr.Read(array, num, 1) < 0)
				{
					throw new Exception("TTC Error");
				}
				if (array[num++] == 0)
				{
					break;
				}
			}
			byte[] array2;
			if (array.Length == --num)
			{
				array2 = array;
			}
			else
			{
				array2 = new byte[num];
				Buffer.BlockCopy(array, 0, array2, 0, num);
			}
			return array2;
		}

		internal byte[] UnmarshalCHR(int retLength)
		{
			byte[] array = null;
			if (m_typeRepresentation.ConversionRequired)
			{
				array = UnmarshalCLR(retLength, retLen);
				if (array.Length != retLen[0])
				{
					byte[] array2 = new byte[retLen[0]];
					Array.Copy(array, 0, array2, 0, retLen[0]);
					array = array2;
				}
			}
			else
			{
				array = GetNBytes(retLength);
			}
			return array;
		}

		internal int GetNBytes(byte[] buf, int off, int len)
		{
			int num = m_oraBufRdr.Read(buf, off, len);
			if (num < 0)
			{
				throw new Exception("TTC Error");
			}
			return num;
		}

		internal byte[] GetNBytes(int n)
		{
			byte[] array = new byte[n];
			if (m_oraBufRdr.Read(array) < 0)
			{
				throw new Exception("TTC Error");
			}
			return array;
		}

		internal byte[] UnmarshalNBytes(int length)
		{
			byte[] array = new byte[length];
			if (length > 0 && m_oraBufRdr.Read(array) < 0)
			{
				throw new Exception("TTC Error");
			}
			return array;
		}

		internal int UnmarshalNBytes(byte[] buf, int off, int n)
		{
			int i;
			for (i = 0; i < n; i += GetNBytes(buf, off + i, n - i))
			{
			}
			return i;
		}

		internal int UnmarshalUCS2(byte[] ucs2Char, long offset)
		{
			int num = UnmarshalUB2();
			tmpBuffer2[0] = (byte)((num & 0xFF00) >> 8);
			tmpBuffer2[1] = (byte)((uint)num & 0xFFu);
			if (offset + 1 < ucs2Char.Length)
			{
				ucs2Char[(int)offset] = tmpBuffer2[0];
				ucs2Char[(int)offset + 1] = tmpBuffer2[1];
			}
			if (tmpBuffer2[0] != 0)
			{
				return 3;
			}
			if (tmpBuffer2[1] != 0)
			{
				return 2;
			}
			return 1;
		}

		internal byte[] UnmarshalDALC(bool bIgnoreData, int[] actualLen = null)
		{
			byte[] array = null;
			long num = UnmarshalUB4();
			num &= 0xFFFFFFFFu & num;
			if (num > 0)
			{
				if (bIgnoreData)
				{
					int length = 0;
					UnmarshalCLR_ScanOnly((int)num, out var _, ref length);
				}
				else
				{
					if (actualLen == null)
					{
						actualLen = retLen;
					}
					array = UnmarshalCLR((int)num, actualLen);
					if (array == null)
					{
						throw new Exception("TTC Error");
					}
				}
			}
			return array;
		}

		internal byte[] UnmarshalCLRforREFS(bool bIgnoreData = false)
		{
			int num = 0;
			int num2 = 0;
			byte[] array = null;
			ArrayList arrayList = null;
			if (!bIgnoreData)
			{
				arrayList = new ArrayList();
			}
			short num3 = UnmarshalUB1();
			if (num3 < 0)
			{
				throw new Exception("TTC Error");
			}
			if (num3 == 0)
			{
				return null;
			}
			if (!EscapeSequenceNull(num3))
			{
				if (num3 == 254)
				{
					while ((num = (m_bUseBigCLRChunks ? UnmarshalSB4() : UnmarshalUB1())) > 0)
					{
						if (bIgnoreData)
						{
							UnmarshalBuffer_ScanOnly(num);
							continue;
						}
						num2 += num;
						byte[] array2 = new byte[num];
						UnmarshalBuffer(array2, 0, num);
						arrayList.Add(array2);
					}
				}
				else if (bIgnoreData)
				{
					UnmarshalBuffer_ScanOnly(num3);
				}
				else
				{
					num2 = num3;
					byte[] array3 = new byte[num3];
					UnmarshalBuffer(array3, 0, num3);
					arrayList.Add(array3);
				}
				if (!bIgnoreData)
				{
					array = new byte[num2];
					int num4 = 0;
					for (int i = 0; i < arrayList.Count; i++)
					{
						int num5 = ((byte[])arrayList[i]).Length;
						Buffer.BlockCopy((byte[])arrayList[i], 0, array, num4, num5);
						num4 += num5;
					}
				}
			}
			else
			{
				array = null;
			}
			return array;
		}

		internal void UnmarshalKeywordValuePair(out int keyword, out string stringValue, out byte[] binaryValue)
		{
			keyword = 0;
			stringValue = null;
			binaryValue = null;
			int[] array = new int[1];
			int num = UnmarshalUB2();
			if (num != 0)
			{
				byte[] bytes = new byte[num];
				UnmarshalCLR(bytes, 0, array);
				stringValue = m_dbCharSetConv.ConvertBytesToString(bytes, 0, array[0]);
			}
			int num2 = UnmarshalUB2();
			if (num2 != 0)
			{
				binaryValue = new byte[num2];
				UnmarshalCLR(binaryValue, 0, array);
			}
			keyword = UnmarshalUB2();
		}

		private void AddPointer(byte val)
		{
			if ((m_typeRepresentation.m_representationArray[4] & 1) > 0)
			{
				m_oraBufWriter.Write(val);
				return;
			}
			byte b = ValueToBuffer(val, tmpBuffer4, 4);
			if (b != 0)
			{
				m_oraBufWriter.Write(tmpBuffer4, 0, b);
			}
		}

		private long BufferToValue(byte repOffset, bool bIgnoreData = false)
		{
			byte[] dataBuffer = null;
			int offset = 0;
			int bufLength = 0;
			long num = 0L;
			bool flag = false;
			byte b = m_typeRepresentation.m_representationArray[repOffset];
			flag = m_oraBufRdr.ReadLengthAndData(repOffset, b, out dataBuffer, ref offset, ref bufLength, bIgnoreData);
			if (bIgnoreData)
			{
				return num;
			}
			switch (bufLength)
			{
			case 1:
				num = dataBuffer[offset];
				break;
			case 2:
				num = (((b & 2) <= 0) ? (dataBuffer[offset + 1] | (dataBuffer[offset] << 8)) : (dataBuffer[offset] | (dataBuffer[offset + 1] << 8)));
				break;
			case 3:
				num = (((b & 2) <= 0) ? (dataBuffer[offset + 2] | (dataBuffer[offset + 1] << 8) | (dataBuffer[offset] << 16)) : (dataBuffer[offset] | (dataBuffer[offset + 1] << 8) | (dataBuffer[offset + 2] << 16)));
				break;
			case 4:
				num = (((b & 2) <= 0) ? (dataBuffer[offset + 3] | (dataBuffer[offset + 2] << 8) | (dataBuffer[offset + 1] << 16) | (dataBuffer[offset] << 24)) : (dataBuffer[offset] | (dataBuffer[offset + 1] << 8) | (dataBuffer[offset + 2] << 16) | (dataBuffer[offset + 3] << 24)));
				break;
			case 5:
				num = (((b & 2) <= 0) ? (dataBuffer[offset + 4] | (dataBuffer[offset + 3] << 8) | (dataBuffer[offset + 2] << 16) | (dataBuffer[offset + 1] << 24) | dataBuffer[offset]) : (dataBuffer[offset] | (dataBuffer[offset + 1] << 8) | (dataBuffer[offset + 2] << 16) | (dataBuffer[offset + 3] << 24) | dataBuffer[offset + 4]));
				break;
			case 6:
				num = (((b & 2) <= 0) ? (dataBuffer[offset + 5] | (dataBuffer[offset + 4] << 8) | (dataBuffer[offset + 3] << 16) | (dataBuffer[offset + 2] << 24) | dataBuffer[offset + 1] | (dataBuffer[offset] << 8)) : (dataBuffer[offset] | (dataBuffer[offset + 1] << 8) | (dataBuffer[offset + 2] << 16) | (dataBuffer[offset + 3] << 24) | dataBuffer[offset + 4] | (dataBuffer[offset + 5] << 8)));
				break;
			case 7:
				num = (((b & 2) <= 0) ? (dataBuffer[offset + 6] | (dataBuffer[offset + 5] << 8) | (dataBuffer[offset + 4] << 16) | (dataBuffer[offset + 3] << 24) | dataBuffer[offset + 2] | (dataBuffer[offset + 1] << 8) | (dataBuffer[offset] << 16)) : (dataBuffer[offset] | (dataBuffer[offset + 1] << 8) | (dataBuffer[offset + 2] << 16) | (dataBuffer[offset + 3] << 24) | dataBuffer[offset + 4] | (dataBuffer[offset + 5] << 8) | (dataBuffer[offset + 6] << 16)));
				break;
			case 8:
				num = (((b & 2) <= 0) ? (dataBuffer[offset + 7] | (dataBuffer[offset + 6] << 8) | (dataBuffer[offset + 5] << 16) | (dataBuffer[offset + 4] << 24) | dataBuffer[offset + 3] | (dataBuffer[offset + 2] << 8) | (dataBuffer[offset + 1] << 16) | (dataBuffer[offset] << 24)) : (dataBuffer[offset] | (dataBuffer[offset + 1] << 8) | (dataBuffer[offset + 2] << 16) | (dataBuffer[offset + 3] << 24) | dataBuffer[offset + 4] | (dataBuffer[offset + 5] << 8) | (dataBuffer[offset + 6] << 16) | (dataBuffer[offset + 7] << 24)));
				break;
			}
			if (flag)
			{
				num = -num;
			}
			return num;
		}

		internal byte GetNoOfBytesToBeWritten(int value, byte repOffset)
		{
			bool flag = true;
			byte b = 0;
			byte b2 = 0;
			for (int num = 3; num >= 0; num--)
			{
				b2 = (byte)HelperClass.URShift(value, 8 * num);
				if ((m_typeRepresentation.m_representationArray[repOffset] & 1) > 0)
				{
					if (!flag || b2 != 0)
					{
						flag = false;
						b = (byte)(b + 1);
					}
				}
				else
				{
					b = (byte)(b + 1);
				}
			}
			return b;
		}

		private byte ValueToBuffer(long value, byte[] outBuffer, byte repOffset)
		{
			bool flag = true;
			byte b = 0;
			for (int num = outBuffer.Length - 1; num >= 0; num--)
			{
				outBuffer[b] = (byte)HelperClass.URShift(value, 8 * num);
				if ((m_typeRepresentation.m_representationArray[repOffset] & 1) > 0)
				{
					if (!flag || outBuffer[b] != 0)
					{
						flag = false;
						b = (byte)(b + 1);
					}
				}
				else
				{
					b = (byte)(b + 1);
				}
			}
			if ((m_typeRepresentation.m_representationArray[repOffset] & 1) > 0)
			{
				m_oraBufWriter.Write(b);
			}
			if ((m_typeRepresentation.m_representationArray[repOffset] & 2) > 0)
			{
				ReverseArray(outBuffer, b, 0);
			}
			return b;
		}

		internal bool EscapeSequenceNull(int bytes)
		{
			bool result = false;
			switch (bytes)
			{
			case 0:
				result = true;
				break;
			case 253:
				throw new Exception("TTC Error");
			case 255:
				result = true;
				break;
			}
			return result;
		}

		private void ReverseArray(byte[] buffer, int bytes, int offset)
		{
			bytes += 2 * offset;
			for (int i = offset; i < bytes / 2; i++)
			{
				byte b = buffer[i];
				buffer[i] = buffer[bytes - 1 - i];
				buffer[bytes - 1 - i] = b;
			}
		}

		internal int ProcessIndicator(bool isNull, int dataSize)
		{
			short num = UnmarshalSB2();
			int result = -1;
			if (!isNull)
			{
				result = ((num == 0) ? dataSize : ((-1 != num) ? num : num));
			}
			return result;
		}

		internal void UnmarshalCLR_ColData(int maxSize)
		{
			int num = 0;
			int num2 = 0;
			int num3 = 0;
			int num4 = 0;
			bool flag = false;
			int num5 = -1;
			int num6 = UnmarshalUB1();
			if (num6 < 0)
			{
				throw new Exception("TTC Error");
			}
			if (EscapeSequenceNull(num6))
			{
				m_oraBufRdr.m_colDataStartOffset[m_oraBufRdr.m_colDataStartOffsetIndexToUpdate] = -1;
				return;
			}
			if (num6 != 254)
			{
				if (num6 > 0)
				{
					num3 = ((maxSize < num6) ? maxSize : num6);
					num2 = UnmarshalBuffer_ScanOnly(num3);
					num4 += num3;
					int num7 = num6 - num3;
					if (num7 > 0)
					{
						UnmarshalBuffer_ScanOnly(num7);
					}
				}
				return;
			}
			num5 = -1;
			bool flag2 = false;
			while (!flag2)
			{
				if (num5 != -1)
				{
					num6 = ((!m_bUseBigCLRChunks) ? UnmarshalUB1() : UnmarshalSB4());
					if (num6 <= 0)
					{
						flag2 = true;
						continue;
					}
				}
				if (num6 == 254)
				{
					switch (num5)
					{
					case -1:
						num5 = 1;
						continue;
					case 1:
						num5 = 0;
						break;
					case 0:
						if (flag)
						{
							num5 = 0;
							break;
						}
						num5 = 0;
						continue;
					}
				}
				if (num2 == -1)
				{
					UnmarshalBuffer_ScanOnly(num6);
				}
				else
				{
					num = num6;
					if (num > 0)
					{
						num3 = ((maxSize - num4 < num) ? (maxSize - num4) : num);
						num2 = UnmarshalBuffer_ScanOnly(num3);
						num4 += num3;
						int num8 = num - num3;
						if (num8 > 0)
						{
							UnmarshalBuffer_ScanOnly(num8);
						}
					}
				}
				num5 = 0;
				if (num6 > 252)
				{
					flag = true;
				}
			}
		}

		internal void UnmarshalCLR_ScanOnly(int maxSize, out List<ArraySegment<byte>> dataSegments, ref int length)
		{
			int num = 0;
			int num2 = 0;
			int num3 = 0;
			int num4 = 0;
			bool flag = false;
			try
			{
				dataSegments = null;
				length = 0;
				int num5 = -1;
				int num6 = UnmarshalUB1();
				if (num6 < 0)
				{
					throw new Exception("TTC Error");
				}
				if (EscapeSequenceNull(num6))
				{
					length = 0;
					return;
				}
				if (num6 != 254)
				{
					if (num6 > 0)
					{
						num3 = ((maxSize < num6) ? maxSize : num6);
						num2 = UnmarshalBuffer_ScanOnly(num3);
						num4 += num3;
						int num7 = num6 - num3;
						if (num7 > 0)
						{
							UnmarshalBuffer_ScanOnly(num7);
						}
					}
					return;
				}
				num5 = -1;
				bool flag2 = false;
				while (!flag2)
				{
					if (num5 != -1)
					{
						num6 = ((!m_bUseBigCLRChunks) ? UnmarshalUB1() : UnmarshalSB4());
						if (num6 <= 0)
						{
							flag2 = true;
							continue;
						}
					}
					if (num6 == 254)
					{
						switch (num5)
						{
						case -1:
							num5 = 1;
							continue;
						case 1:
							num5 = 0;
							break;
						case 0:
							if (flag)
							{
								num5 = 0;
								break;
							}
							num5 = 0;
							continue;
						}
					}
					if (num2 == -1)
					{
						UnmarshalBuffer_ScanOnly(num6);
					}
					else
					{
						num = num6;
						if (num > 0)
						{
							num3 = ((maxSize - num4 < num) ? (maxSize - num4) : num);
							num2 = UnmarshalBuffer_ScanOnly(num3);
							num4 += num3;
							int num8 = num - num3;
							if (num8 > 0)
							{
								UnmarshalBuffer_ScanOnly(num8);
							}
						}
					}
					num5 = 0;
					if (num6 > 252)
					{
						flag = true;
					}
				}
			}
			finally
			{
				length = num4;
				dataSegments = m_oraBufRdr.m_dataSegments;
			}
		}

		internal int GetNBytes_ScanOnly(int len)
		{
			int num = m_oraBufRdr.Read(null, 0, len);
			if (num < 0)
			{
				throw new Exception("TTC Error");
			}
			return num;
		}

		internal int UnmarshalNBytes_ScanOnly(int n)
		{
			int i;
			for (i = 0; i < n; i += GetNBytes_ScanOnly(n))
			{
			}
			return i;
		}

		internal int UnmarshalBuffer_ScanOnly(int len)
		{
			int num = m_oraBufRdr.Read(null, 0, len);
			if (num < 0)
			{
				throw new Exception("TTC Error");
			}
			return num;
		}

		internal void ProcessReset()
		{
			try
			{
				m_oracleCommunication.Reset();
				if (UnmarshalSB1() == 4)
				{
					TTCErrorObject.Initialize();
					TTCErrorObject.ReadErrorMessage();
					return;
				}
				throw new Exception("OraBufWriter:ReadResetResponse - Unexpected Packet received.");
			}
			catch (NetworkException)
			{
				throw;
			}
		}
	}
}
